/**
 * 
 */
package com.yidu.view;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yidu.bean.OrdIncome;
import com.yidu.bean.TbImage;
import com.yidu.bean.TbManager;
import com.yidu.bean.TbShop;
import com.yidu.commom.PhoneSendTool;
import com.yidu.commom.Tool;
import com.yidu.message.Message;
import com.yidu.message.Pages;
import com.yidu.service.TbImageService;
import com.yidu.service.TbShopService;

/**
 * 
 * @author liutao
 * @data 2018年7月16日
 * @version 1.0
 */
@Controller
@RequestMapping("/shop")
public class TbShopAction {
	//打印控制台
	Logger logger = Logger.getLogger(TbShopAction.class);

	//消息信息类
	Message msg = new Message();

	//json
	ObjectMapper mapper = new ObjectMapper();

	@Resource
	private TbShopService tbShopServiceImpl ;

	//图片 service
	@Resource
	private TbImageService imageService;


	@RequestMapping("/tiaozhuan")
	public String tiaozhuan(){
		return "shop/shop";
	}

	@RequestMapping("/tiao")
	public String tiao(){
		logger.info("进入订单结算页面...");
		return "background/center/checkout";
	}
	/**
	 * 由于配置原因 url只支持*.action结尾 <br>
	 * 所以需要配置路径
	 * @return
	 */
	@RequestMapping("/show")
	public String tiaozhuans(){
		logger.info("进入店家表...");
		return "background/shop/shop";
	}

	@RequestMapping("/findById")
	@ResponseBody
	public TbShop findById(String shopId,HttpSession session){
		logger.info("这是一个根据id查询店面的方法");
		session.setAttribute("shop", shopId);
		TbShop shop = tbShopServiceImpl.findById(shopId);
		return shop;
	}

	/**
	 * 自行判断 增加 修改<br>
	 * 根据ID是否存在
	 * @param member
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping(value="/saveOrUpdate",method=RequestMethod.POST)
	@ResponseBody
	public Object saveOrUpdate(HttpServletResponse response, HttpServletRequest request ,TbShop shop,String type ) throws IOException {

		logger.info("进入增加修改方法...");

		logger.info(mapper.writeValueAsString(shop));

	//	logger.info(address1+"  "+address2+" "+address3);
		
	 
		int num = 0;
		 

		//判断是否存在id 

		if(!Tool.isEmpty(shop.getShopId())){
			//logger.info("");
			//存在 执行修改 
			num = tbShopServiceImpl.update(shop);

			//	num = imageService.update(image);

		}else{
			//反之 执行增加
			//自行增加主键
			shop.setShopId(Tool.getRandomString(32));
			shop.setIsva(0);
			num = tbShopServiceImpl.save(shop);
			logger.info("====开店审核判断====type="+type);
			if (!Tool.isEmpty(type)){
				response.sendRedirect("page/audit.html");
			}
		}

		//得到业务ID
		TbImage img = imageService.findById(shop.getShopId()); 

		logger.info(mapper.writeValueAsString(img));

		//判断是否为空
		if(!StringUtils.isEmpty(img)){
			//修改图片
			/*image.setImgId(img.getImgId());
	 		image.setYewuId(member.getMemId());*/
			img.setIamge(shop.getFileName());
			img.setSort(1);
			num = imageService.update(img);
		}else{
			//增加 图片
			TbImage image = new TbImage();
			image.setYewuId(shop.getShopId());
			image.setImgId(Tool.getRandomString(32));
			image.setIamge(shop.getFileName());
			image.setSort(1);
			num = imageService.save(image);
		}

		//根据 操作返回
		msg.setNum(num);
		if(num>0){
			msg.setMsg("操作成功!");
		}else{
			msg.setMsg("操作失败!");
		}
		return msg;
	}

	/**
	 *  接受前台 传递过来的参数<br>
	 *  执行删除方法
	 * @param memId
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public  Object delete(String shopId){


		logger.info("进入删除方法....");
		int row = tbShopServiceImpl.delete(shopId);
		//HttpServletResponse response = ServletActionContext.getResponse();
		//response.setCharacterEncoding("utf-8");
		if(row>0){
			msg.setMsg("删除成功!");
		}else{ 
			msg.setMsg("删除失败!");
		} 
		return msg;
	}

	
	
	@RequestMapping("/findVague")
	@ResponseBody
	public Object findVague(HttpServletRequest request,Pages pages,TbShop shop) throws IOException{
		logger.info("进入模糊查询...");

		logger.info(mapper.writeValueAsString(pages));

		logger.info(mapper.writeValueAsString(shop));

		/*String limit = (String) request.getAttribute("limit");

		if (!Tool.isEmpty(limit)) pages.setLimit(12);*/

		//得到查询结果集
		List<TbShop> list = tbShopServiceImpl.findVagues(pages, shop);

		for (int i = 0; i < list.size(); i++) {
			TbShop shops = list.get(i);

			//得到业务ID 查询出图片
			TbImage img = imageService.findById(shops.getShopId());

			//判断是否为空  
			if(!StringUtils.isEmpty(img)){

				shops.setFileName("../"+img.getIamge());
			}

			list.set(i, shops);

		}
		logger.info(mapper.writeValueAsString(list));

		//得到结果集行数
		int row= tbShopServiceImpl.findCounts(shop);

		//由于前端所需条件
		Map<String, Object> map = new HashMap<>();

		map.put("code", 0);
		map.put("msg","");
		map.put("count",row);
		map.put("data", list);
		return map;
	}

	@RequestMapping("/isvaUp")
	@ResponseBody
	private Object isvaUp(TbShop shop,HttpSession session){
		logger.info("审核..");
		shop.setIsva(1);

		int num = tbShopServiceImpl.isvaUp(shop);

		msg.setNum(num);
		if(num>0){
			msg.setMsg("审核成功!");
		}else{
			msg.setMsg("审核失败!");
		}

		TbShop shops = tbShopServiceImpl.findById(shop.getShopId());
		//将店铺存入session
		session.setAttribute("showShop",shops);
		
		return msg;

	}

	/**
	 * 根据ID查询对象类型
	 * 
	 * @param id
	 * @param response
	 * @return
	 */
	@RequestMapping("/showUpdate")
	@ResponseBody
	public Object showUpdate(String shopId){
		logger.info("进入根据ID查询...");
		TbShop member = tbShopServiceImpl.findById(shopId);
		return member;

	}

	/**
	 * 获取开店审核的手机验证码
	 * @param session 会话状态
	 * @param phone 要发送信息的手机号
	 * @return 手机验证码（String）
	 */
	@RequestMapping("/phoneCode")
	@ResponseBody
	public String phoneCode(HttpSession session, String phone){
		String rdCode = PhoneSendTool.execute(phone);
		session.setAttribute("phone", phone);
		logger.info("获取手机号："+phone+" 的验证码是："+rdCode);
		return rdCode;
	}

	/**
	 * 得到存在session中的店长手机号
	 * @param session 会话状态
	 * @return 店长手机号（String）
	 */
	@RequestMapping("/phone")
	@ResponseBody
	public String phone(HttpSession session){
		String phone = (String) session.getAttribute("phone");
		logger.info("获取手机号："+phone);
		return phone;
	}

	@RequestMapping("/salesOfShop")
	@ResponseBody
	public int[] salesOfShop(HttpSession session,String year) {
		TbManager manager = (TbManager) session.getAttribute("manager");
		String id = manager.getShopId();
		List<OrdIncome> list = tbShopServiceImpl.salesOfShop(year,id);
		int[] arr=new int[12];
		for (int i = 0; i < arr.length; i++) {
			arr[i] = list.get(i).getShopIncome();
		}
		return arr;
	}
	@RequestMapping("/findAll")
	@ResponseBody
	public Object findAll(){
		
		 List<TbShop> list = tbShopServiceImpl.findAll();
		 
		 return list;
	}
}
